package cn.lngaosu.lwyd.module.integration.controller.admin.weixinarticleanalysis;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.weixinarticleanalysis.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinarticleanalysis.WeixinArticleAnalysisDO;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.weixinarticleanalysis.WeixinArticleAnalysisSourceDO;
import cn.lngaosu.lwyd.module.integration.service.weixinarticleanalysis.WeixinArticleAnalysisService;

@Tag(name = "管理后台 - 微信文章分析")
@RestController
@RequestMapping("/integration/weixin-article-analysis")
@Validated
public class WeixinArticleAnalysisController {

    @Resource
    private WeixinArticleAnalysisService weixinArticleAnalysisService;

    @PostMapping("/create")
    @Operation(summary = "创建微信文章分析")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:create')")
    public CommonResult<Long> createWeixinArticleAnalysis(@Valid @RequestBody WeixinArticleAnalysisSaveReqVO createReqVO) {
        return success(weixinArticleAnalysisService.createWeixinArticleAnalysis(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新微信文章分析")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:update')")
    public CommonResult<Boolean> updateWeixinArticleAnalysis(@Valid @RequestBody WeixinArticleAnalysisSaveReqVO updateReqVO) {
        weixinArticleAnalysisService.updateWeixinArticleAnalysis(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除微信文章分析")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:delete')")
    public CommonResult<Boolean> deleteWeixinArticleAnalysis(@RequestParam("id") Long id) {
        weixinArticleAnalysisService.deleteWeixinArticleAnalysis(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除微信文章分析")
                @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:delete')")
    public CommonResult<Boolean> deleteWeixinArticleAnalysisList(@RequestParam("ids") List<Long> ids) {
        weixinArticleAnalysisService.deleteWeixinArticleAnalysisListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信文章分析")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:query')")
    public CommonResult<WeixinArticleAnalysisRespVO> getWeixinArticleAnalysis(@RequestParam("id") Long id) {
        WeixinArticleAnalysisDO weixinArticleAnalysis = weixinArticleAnalysisService.getWeixinArticleAnalysis(id);
        return success(BeanUtils.toBean(weixinArticleAnalysis, WeixinArticleAnalysisRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信文章分析分页")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:query')")
    public CommonResult<PageResult<WeixinArticleAnalysisRespVO>> getWeixinArticleAnalysisPage(@Valid WeixinArticleAnalysisPageReqVO pageReqVO) {
        PageResult<WeixinArticleAnalysisDO> pageResult = weixinArticleAnalysisService.getWeixinArticleAnalysisPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WeixinArticleAnalysisRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信文章分析 Excel")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWeixinArticleAnalysisExcel(@Valid WeixinArticleAnalysisPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeixinArticleAnalysisDO> list = weixinArticleAnalysisService.getWeixinArticleAnalysisPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信文章分析.xls", "数据", WeixinArticleAnalysisRespVO.class,
                        BeanUtils.toBean(list, WeixinArticleAnalysisRespVO.class));
    }

    // ==================== 子表（微信文章分析子表(按渠道区分)） ====================

    @GetMapping("/weixin-article-analysis-source/list-by-analysis-id")
    @Operation(summary = "获得微信文章分析子表(按渠道区分)列表")
    @Parameter(name = "analysisId", description = "分析表数据主键")
    @PreAuthorize("@ss.hasPermission('integration:weixin-article-analysis:query')")
    public CommonResult<List<WeixinArticleAnalysisSourceDO>> getWeixinArticleAnalysisSourceListByAnalysisId(@RequestParam("analysisId") Long analysisId) {
        return success(weixinArticleAnalysisService.getWeixinArticleAnalysisSourceListByAnalysisId(analysisId));
    }

}